﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp.对比较
{
    /// <summary>
    /// 定义一个点
    /// </summary>
    class Point
    {
        public int x { get; set; }
        public int y { get; set; }
        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    /// <summary>
    /// 定义一个点,并重写对象与对象是否相等的方法
    /// 可用于判断对象是否相等
    /// eg: 
    ///     obj1 == obj2
    ///     obj1.Equals(obj2)
    /// </summary>
    class TestPoint : IEquatable<TestPoint>
    {
        public int x { get; set; }
        public int y { get; set; }
        public TestPoint(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        /// <summary>
        /// 重载 == 运算符
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static bool operator ==(TestPoint p1, TestPoint p2)
        {
            return (p1.x == p2.x) && (p1.y == p2.y);
        }

        /// <summary>
        /// 重载 != 运算符
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static bool operator !=(TestPoint p1, TestPoint p2)
        {
            return (p1.x != p2.x) || (p1.y != p2.y);
        }

        /// <summary>
        /// 重写Equals(object obj)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            return this.Equals(obj as TestPoint);
        }

        /// <summary>
        /// 重写 计算对象的哈希值方法（自定义 这里只是示范）
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.x.GetHashCode() + this.y.GetHashCode();
        }

        /// <summary>
        /// 继承定义Equals<T>方法
        /// 需要继承接口IEquatable<T>
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(TestPoint other)
        {
            return (this.x == other.x) && (this.y == other.y);
        }

    }
    public class TestClass
    {
        public static void classEqual()
        {
            Point p1 = new Point(2, 1);
            Point p2 = new Point(2, 1);
            Console.WriteLine(p1 == p2);//False
            Console.WriteLine(p1.Equals(p2));//False
            // ReferenceEquals 方法用于对象的引用是否相等
            // ReferenceEquals 不能重写 注意
            Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
            p1 = p2;
            Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True

            TestPoint p3 = new TestPoint(2, 1);
            TestPoint p4 = new TestPoint(2, 1);
            Console.WriteLine(p3 == p4);//True
            Console.WriteLine(p3.Equals(p4));//True
            // ReferenceEquals 方法用于对象的引用是否相等
            // ReferenceEquals 不能重写 注意
            Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//False
            p3 = p4;
            Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//True

            List<Point> list1 = new List<Point>() { new Point(1, 1), new Point(1, 2), new Point(1, 3), new Point(1, 4), new Point(1, 5), new Point(1, 6) };
            var result1 = list1.Where(M => M == new Point(1, 3));
            List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(1, 1), new TestPoint(1, 2), new TestPoint(1, 3), new TestPoint(1, 4), new TestPoint(1, 5), new TestPoint(1, 6) };
            var result2 = list2.Where(M => M == new TestPoint(1, 3));
            Console.Read();
        }
    }
}
